﻿using System.Text;
using System.Data.SQLite;

namespace PersonalAccounting.DataAccess
{
    public class SQLiteConn
    {

        #region Singleton

        private static SQLiteConn _instance;

        private SQLiteConn()
        {
            _connection = new SQLiteConnection(BuildConnectionString());
        }

        public static SQLiteConn GetInstance()
        {
            if (_instance == null)
                _instance = new SQLiteConn();

            return _instance;
        }

        #endregion


        #region Attributes

        private SQLiteConnection _connection;

        #endregion

        #region Properties

        public SQLiteConnection Connection
        {
            get { return _connection; }
            set { _connection = value; }
        }

        #endregion

        #region Methods

        public void Connect()
        {
            try
            {
                _connection.Open();
            }
            catch 
            {
                throw new ConnectionException();
            }
        }

        public void Disconnect()
        {
            try
            {
                _connection.Close();
            }
            catch
            {
                throw new ConnectionException();
            }
        }

        #endregion


        #region Private Methods

        private string BuildConnectionString()
        {
            StringBuilder cs = new StringBuilder();

            cs.Append("Data Source=D:\\SQLite\\PersonalAccounting.db;");
            cs.Append("Version=3;");
            cs.Append("Pooling=False;");
            cs.Append("Synchronous=Full;");

            return cs.ToString();
        }

        #endregion
    }
}